﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Point = Microsoft.Xna.Framework.Point;

namespace VikingArcade_v2._0
{
    public class UserInterface : IDrawable, IAnimated
    {
        #region IDrawable Members

        public IDrawer GetDrawer()
        {
            return new UserInterfaceDrawer(this);
        }

        #endregion

        #region IAnimated Members

        public IAnimator GetAnimator()
        {
            throw new NotImplementedException();
        }

        #endregion
        
        public UserInterface()
        {
            lastInput = Keyboard.GetState().GetPressedKeys().ToList();
            lastMouseState = Mouse.GetState();

            InitializeControls();
        }

        #region InputLogic

        private List<Keys> lastInput;
        private MouseState lastMouseState;

        public void UpdateInput()
        {
            foreach (UserInterfaceControl userInterfaceControl in controls)
            {
                bool left = Mouse.GetState().LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released;
                bool right = Mouse.GetState().RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released;
                userInterfaceControl.UpdateMouseInput(new Point(Mouse.GetState().X, Mouse.GetState().Y), left, right);
            }
            lastMouseState = Mouse.GetState();
            lastInput = Keyboard.GetState().GetPressedKeys().ToList();
        }

        public bool IsKeyPressed(Keys key)
        {
            return Keyboard.GetState().GetPressedKeys().ToList().Contains(key) && !lastInput.Contains(key);
        }

        #endregion

        #region Design

        protected List<UserInterfaceControl> controls;

        public IEnumerable<UserInterfaceControl> Controls
        {
            get
            {
                return controls.ToArray();
            }
        }

        protected virtual void InitializeControls()
        {
            controls = new List<UserInterfaceControl>();
        }

        /// <summary>
        /// Dopasowuje kontrolki do nowego rozmiaru obszaru wyswietlania
        /// </summary>
        /// <param name="oldSize">Stary rozmiar obszaru</param>
        /// <param name="newSize">Nowy rozmiar obszaru</param>
        public void ResizeControls(Size oldSize, Size newSize)
        {
            foreach (UserInterfaceControl userInterfaceControl in controls)
            {
                userInterfaceControl.AdjustLayout(oldSize, newSize);
            }
        }


        #endregion
    }
}
